package com.hitqz.robot.biz.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hitqz.robot.api.business.entity.RobotEntity;
import com.hitqz.robot.api.business.entity.RobotGroupEntity;
import com.hitqz.robot.biz.service.RobotGroupService;
import com.hitqz.robot.biz.service.RobotService;
import com.hitqz.robot.common.core.util.R;
import com.hitqz.robot.common.log.annotation.SysLog;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 机器人调度组管理
 *
 * @author robot
 * @date 2024-06-06 15:17:22
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/robotGroup" )
@Tag(description = "robotGroup" , name = "机器人调度组管理管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RobotGroupController {

    private final  RobotGroupService robotGroupService;

	private final RobotService robotService;

    @Operation(summary = "分页查询有在线机器人的调度组" , description = "分页查询有在线机器人的调度组" )
    @GetMapping("/onlineGroup")
    public R<List<RobotGroupEntity>> onlineGroup() {
        List<RobotGroupEntity> robotGroupEntities = robotGroupService.list();
        //Cache<String, RobotClientSummaryDto> posInfoDtoCache = SpringUtil.getBean("robotCache");
        //List<RobotGroupEntity> robotGroupEntities = Lists.newArrayList();
        //for (String robotSn : CollUtil.emptyIfNull(posInfoDtoCache.asMap().keySet())) {
        //    RobotEntity robotEntity = robotService.selectByRobotSn(robotSn);
        //    List<RobotGroupEntity> exist = robotGroupEntities.stream().filter(t-> t.getId().equals(robotEntity.getGroupId())).collect(Collectors.toList());
        //    if (CollUtil.isEmpty(exist)) {
        //        robotGroupEntities.add(robotGroupService.getById(robotEntity.getGroupId()));
        //    };
        //}
        return R.ok(robotGroupEntities);
    }



    /**
     * 分页查询
     * @param page 分页对象
     * @param robotGroup 机器人调度组管理
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    public R<Page<RobotGroupEntity>> getRobotGroupPage(@ParameterObject Page page, @ParameterObject RobotGroupEntity robotGroup) {
        LambdaQueryWrapper<RobotGroupEntity> wrapper = Wrappers.lambdaQuery();
		return R.ok(robotGroupService.page(page, wrapper));
    }

	/**
	 * 查询全部调度组
	 */
	@Operation(summary = "获取所有调度组" , description = "获取所有调度组" )
	@GetMapping("/list")
	public R<List<RobotGroupEntity>> list() {
		return R.ok(robotGroupService.list());
	}


    /**
     * 通过id查询机器人调度组管理
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    public R<RobotGroupEntity> getById(@PathVariable("id" ) Integer id) {
		id = Math.abs(id);
        return R.ok(robotGroupService.getById(id));
    }

    /**
     * 新增机器人调度组管理
     * @param robotGroup 机器人调度组管理
     * @return R
     */
    @Operation(summary = "新增机器人调度组管理" , description = "新增机器人调度组管理" )
    @SysLog("新增机器人调度组管理" )
    @PostMapping
    public R save(@RequestBody RobotGroupEntity robotGroup) {
		//robotGroup.setId(null);
        return R.ok(robotGroupService.save(robotGroup));
    }

    /**
     * 修改机器人调度组管理
     * @param robotGroup 机器人调度组管理
     * @return R
     */
    @Operation(summary = "修改机器人调度组管理" , description = "修改机器人调度组管理" )
    @SysLog("修改机器人调度组管理" )
    @PutMapping
    public R updateById(@RequestBody RobotGroupEntity robotGroup) {
        return R.ok(robotGroupService.updateById(robotGroup));
    }

    /**
     * 通过id删除机器人调度组管理
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除机器人调度组管理" , description = "通过id删除机器人调度组管理" )
    @SysLog("通过id删除机器人调度组管理" )
    @DeleteMapping
    public R removeById(@RequestBody Integer[] ids) {
		for (int i = 0; i < ids.length; i++) {
			ids[i] = Math.abs(ids[i]);
		}
		if (ArrayUtil.contains(ids,1)){
			return R.failed("不能删除默认分组");
		}
		List<RobotEntity> list = robotService.list(Wrappers.<RobotEntity>query().lambda().in(RobotEntity::getGroupId, ids));
		if (CollUtil.isNotEmpty(list)){
			return R.failed("请先删除调度组下的机器人再执行操作");
		}
		return R.ok(robotGroupService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param robotGroup 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    public List<RobotGroupEntity> export(RobotGroupEntity robotGroup,Integer[] ids) {
        return robotGroupService.list(Wrappers.lambdaQuery(robotGroup).in(ArrayUtil.isNotEmpty(ids), RobotGroupEntity::getId, ids));
    }
}